package Records.Attributes;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
 *
 * @author Alexander
 */
public class websiteDataExtract {

    private static String[][] TopRss;

    public static String[][] webExtractToArray(String site, String docSelect, String docAttribute, int numOfSongs, String token, String location) {
        System.out.println("websiteDataExtract.webExtractToArray(" + site + ", " + docSelect + ", " + docAttribute + ", " + numOfSongs + ", " + token + ", " + location + ")");

        int counter = 0;
        Document doc = null;
        try {
            doc = Jsoup.connect(site).get();
        } catch (SocketTimeoutException ex) {
            webExtractToArray(site, docSelect, docAttribute, numOfSongs, token, location);
        } catch (MalformedURLException ex) {
            Logger.getLogger(websiteDataExtract.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(websiteDataExtract.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (doc != null) {
            Elements metalinks = doc.select(docSelect);
            TopRss = new String[numOfSongs][2];
            for (Element link : metalinks) {
                String musicLink = link.attr(docAttribute);
                if ("Beatport".equals(location)) {
                    musicLink = parseBeatportJson(musicLink);
                }
                StringTokenizer stringtokenizer = new StringTokenizer(musicLink, token);
                if (stringtokenizer.hasMoreTokens()) {
                    try {
                        TopRss[counter][0] = stringtokenizer.nextToken();
                        TopRss[counter][1] = stringtokenizer.nextToken();
                    } catch (Exception ex) {
                        Logger.getLogger(websiteDataExtract.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                counter++;
            }
        }
        return TopRss;
    }

    public static String parseBeatportJson(String jsonInput) {
        System.out.println("websiteDataExtract.parseBeatportJson(" + jsonInput + ")");

        String title = null;
        String artist = null;
        try {
            // "I want to iterate though the objects in the array..."
            JSONObject outerObject = new JSONObject(jsonInput);
            title = outerObject.getString("name");
            // JSONObject innerObject = outerObject.getJSONObject("artists");
            JSONArray jsonArray = outerObject.getJSONArray("artists");
            JSONObject objectInArray = jsonArray.getJSONObject(0);
            artist = objectInArray.getString("name");
            //System.out.println();
        } catch (JSONException ex) {
            Logger.getLogger(websiteDataExtract.class.getName()).log(Level.SEVERE, null, ex);
        }
        return title + " - " + artist;
    }
}
